import axios from 'axios'
import { createStore } from 'vuex'

export const WORKDIR_UNDEFINED = "UNINITIALIZED"

const initialValues = {
    initialized: false,
    workdir: WORKDIR_UNDEFINED
}
const store = createStore({
    state() {
        return initialValues
    },
    mutations: {
        setWorkdir(state: typeof initialValues, wd: string) {
            state.workdir = wd
        }
    }
})

export const initStore = async (): Promise<void> => {
    const initialInfo: typeof initialValues = (await axios.get("/api/project/initial-info")).data
    store.commit('setWorkdir', initialInfo.workdir)
}

export const getStore = async () => {
    if (store.state.initialized == false) {
        await initStore()
    }
    return store
}

export default store